﻿
using System;
using System.Drawing;
using System.Collections.Generic;

using GECS.Object;

namespace GECS.MapEngine {

	/// <summary>
	/// A layer of the map. Used to draw the objects in the order of depth.
	/// </summary>
    public class MapLayer {

        List<GObject> objects = null;

        /// <summary>
        /// Constructs a new MapLayer object.
        /// </summary>
        public MapLayer() {
            objects = new List<GObject>();
        }

        /// <summary>
        /// Adds an object to this layer.
        /// </summary>
        /// <param name="obj">The object to be added.</param>
        public void AddObject(GObject obj) {
            if (obj != null) {
                objects.Add(obj);
            }
        }
        
        /// <summary>
        /// Renders this layer at the top-left corner of the screen.
        /// </summary>
        /// <param name="g">The graphics context.</param>
        public void Render(Graphics g){
        	Render(g, 0, 0);
        }

        /// <summary>
        /// Renders this layer at a specific position of the screen.
        /// </summary>
        /// <param name="g">The graphics context.</param>
        /// <param name="x">The x-position.</param>
        /// <param name="y">The y-position.</param>
        public void Render(Graphics g, int x, int y) {
            for (int i = 0; i < objects.Count; i++) {
        		GObject obj = objects[i];
                if (!obj.IsAlive()) {
                	objects.Remove(obj);
                } else {
                    if (MapView.IsVisible(obj) && obj.Visible) {
                        float obj_x = obj.X + x;
                        float obj_y = obj.Y + y;
                        g.DrawImage(obj.Image, obj_x, obj_y, obj.Width, obj.Height);
                    }
                }
            }
        }

    }

}
